package rbac.web.tag;

import java.util.Collection;

import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.jstl.core.ConditionalTagSupport;

public class PermissionTag extends ConditionalTagSupport {

	private static final long serialVersionUID = 4684526331526783662L;

	/** 前台传入的 action 串 */
	private String actStr; // 变量名称需要对应 tld 文件里的<attribute><name>元素项.

	// ---=== Constructors ===--- //
	public PermissionTag() {
		super();
		this.init();
	}

	// ---=== Methods ===--- //
	@Override
	public void release() {
		super.release();
		this.init();
	}

	@SuppressWarnings("unchecked")
	@Override
	protected boolean condition() throws JspTagException {
		if (null == actStr || actStr.equals("")) {
			return false;
		}

		Object c = pageContext.getRequest().getAttribute("perSet");
		if(c == null){
		    // 如果没有该值，则直接返回！
		    return false;
		}
		
		if (c instanceof Collection) {
			Collection<String> perSet = (Collection<String>) c;
			return perSet.contains(actStr);
		}

		return false;
	}

	private void init() {
		this.actStr = null;
	}

	/**
	 * @param actStr
	 *            the actStr to set
	 */
	public void setActStr(String actStr) {
		this.actStr = actStr;
	}

}
